# !/usr/bin python3
# encoding: utf-8 -*-
# @file     : grpc_server.py
# @author   : 沙陌 Matongxue_2
# @Time     : 2023/3/23 21:04
# @Copyright: 北京码同学
import time
from concurrent import futures

import grpc

from grpc_study import add_pb2_grpc, add_pb2

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class AddServer(add_pb2_grpc.AddServiceServicer):
    def add(self, request, context):
        print(f'a:{request.a},b:{request.b}')
        c = request.a + request.b
        return add_pb2.AddReply(res=c)


def server():
    # os.system('set GRPC_VERBOSITY=debug')
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    add_pb2_grpc.add_AddServiceServicer_to_server(AddServer(), server)
    server.add_insecure_port('127.0.0.1:33333')
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    server()
